﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StoryTeller.Domain.Repository;
using StoryTeller.Domain;
using System.Text;
using System.IO;
using StoryTellerSite.Models;

namespace StoryTellerSite.Controllers
{
    public abstract class BasePagerController<T> : BaseController where T : BaseDomain
    {
        public PagerModel PagerModel
        {
            get
            {
                if (Session[Controller] == null)
                {
                    Session[Controller] = new PagerModel();
                    if (CountPerPage > 0)
                    {
                        PagerModel.ItemsPerPage = CountPerPage;
                    }
                }

                return (PagerModel)Session[Controller];
            }
        }

        public BasePagerController()
        {

        }

        /// <summary>
        /// página atualm
        /// </summary>
        public int Current { get; set; }
        private int countPerPage;
        public virtual int CountPerPage { get { return countPerPage; } }
        public int Total { get; set; }

        public abstract string PartialViewName { get; }
        public abstract string Controller { get; }

        public abstract string JQuerySelector { get; }

        public abstract bool UsePager { get; }

        public abstract bool Append { get; }

        public abstract IPagerRepository<T> Repository { get; }

        public ICollection<T> Data { get; set; }

        public ActionResult List()
        {

            Pagination<T> p = new Pagination<T>();
            p.PageSize = PagerModel.ItemsPerPage;
            p.PageNumber = 1;
            this.Data = this.Repository.Select(p).List;

            PagerModel.TotalItems = p.TotalItems;
            PagerModel.Controller = this.Controller;

            StringBuilder sb = new StringBuilder();
            sb.Append(BaseController.RenderPartialToString(PartialViewName, Data, this.ControllerContext));
            if (UsePager)
                sb.Append(BaseController.RenderPartialToString(PagerModel.PagerPartialViewName, PagerModel, this.ControllerContext));
            return new ContentResult() { ContentType = "Text/html", Content = sb.ToString(), ContentEncoding = Encoding.UTF8 };
        }

        public ActionResult Filter(string guid)
        {
            return View();
        }

        public ActionResult Pager(int Index)
        {
            Pagination<T> p = new Pagination<T>();
            p.PageSize = CountPerPage;
            p.PageNumber = Index;
            this.Data = this.Repository.Select(p).List;

            PagerModel.Current = Index;
            PagerModel.TotalItems = p.TotalItems;
            PagerModel.Controller = this.Controller;

            StringBuilder sb = new StringBuilder();
            sb.Append(BaseController.RenderPartialToString(PartialViewName, Data, this.ControllerContext));
            if (UsePager)
                sb.Append(BaseController.RenderPartialToString(PagerModel.PagerPartialViewName, PagerModel, this.ControllerContext));

            return new JsonPartial(sb.ToString(), this.JQuerySelector, Append);

        }

        public ActionResult Order(string guid)
        {
            return View();
        }

        public ActionResult Clear(string guid)
        {
            Session[Controller] = null;

            return List();
        }
    }
}
